package com.coffee.order.service.sku.impl;

import com.coffee.admin.service.platform.bean.RechargeRule;
import com.coffee.admin.service.platform.dao.RechargeRuleDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.enums.OrderTypeEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.order.api.sku.query.OrderBalanceQuery;
import com.coffee.order.api.sku.result.OrderBalanceBackendResult;
import com.coffee.order.api.sku.result.OrderBalanceResult;
import com.coffee.order.api.sku.result.OrderResult;
import com.coffee.order.api.sku.service.OrderBalanceService;
import com.coffee.order.service.sku.bean.OrderBalance;
import com.coffee.order.service.sku.dao.OrderBalanceDao;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class OrderBalanceImpl implements OrderBalanceService {
    @Value("${TAG}")
    private String environment;
    @Resource
    private OrderBalanceDao orderBalanceDao;
    @Resource
    private RechargeRuleDao rechargeRuleDao;


    @Override
    public Page<OrderBalanceResult> getPager(OrderBalanceQuery param, PagerInfo pr) {
        Page<OrderBalanceResult> pager = new Page<OrderBalanceResult>(pr.getStart(), pr.getPageSize());
        Integer count = orderBalanceDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<OrderBalance> list = orderBalanceDao.page(param, pr.getStart(), pr.getPageSize());
            pager.setData(DTOUtils.convertList(list, OrderBalanceResult.class));
        }
        return pager;
    }

    @Override
    public Page<OrderBalanceBackendResult> getOrderBalanceBackendResult(OrderBalanceQuery param, PagerInfo pr) {
        Page<OrderBalanceBackendResult> pager = new Page<OrderBalanceBackendResult>(pr.getStart(), pr.getPageSize());
        Integer count = orderBalanceDao.countBank(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<OrderBalanceBackendResult> orderBalanceBackendResults = orderBalanceDao.pageByBackend(param, pr.getStart(), pr.getPageSize());
            pager.setData(orderBalanceBackendResults);
        }
        return pager;
    }

    @Override
    public OrderBalanceResult getDetail(Integer id) {
        OrderBalanceResult orderBalanceResult = new OrderBalanceResult();
        OrderBalance orderBalance = orderBalanceDao.findById(id);
        BeanUtilExt.copyProperties(orderBalanceResult, orderBalance);
        return orderBalanceResult;
    }

    @Override
    public void delete(Integer id) {
        orderBalanceDao.deleteById(id);
    }

    @Override
    public OrderResult addOrUpdate(OrderBalanceResult result) throws BusinessException {
        OrderResult orderResult = new OrderResult();
        RechargeRule rechargeRule = rechargeRuleDao.selectByRechargeAmount(result.getBalanceMoney());
        OrderBalance item = new OrderBalance();
        BeanUtilExt.copyProperties(item, result);
        item.setCreateTime(new Date());
        item.setRechargeType("recharge");
        item.setNotes("充值:" + item.getBalanceMoney());
        item.setStatus("waiting");
        Integer orderBalanceId = orderBalanceDao.insert(item);
        if (orderBalanceId > 0 && rechargeRule != null) {
            OrderBalance giftOrderBalance = new OrderBalance();
            giftOrderBalance.setMerchantId(item.getMerchantId());
            giftOrderBalance.setBalanceMoney(rechargeRule.getGiftAmount());
            giftOrderBalance.setRechargeType("gift");
            giftOrderBalance.setNotes("充值:" + item.getBalanceMoney() + ",赠送:" + rechargeRule.getGiftAmount());
            giftOrderBalance.setCreateTime(new Date());
            giftOrderBalance.setOrderBalanceId(item.getId());
            giftOrderBalance.setStatus("waiting");
            orderBalanceDao.insert(giftOrderBalance);
        }
        orderResult.setPayPrice(item.getBalanceMoney());
        orderResult.setOutOrderId(environment + OrderTypeEnum.BALANCE.getCode() + "-" + item.getId());
        orderResult.setOrderType(OrderTypeEnum.BALANCE.getCode());
        return orderResult;
    }
}
